﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;

namespace SkinableFormsLib
{
	public class LWButton : Button, ILWControl
	{
		private Graphics _lwGraphics;
		private Bitmap _lwBitmap;

		public LWButton()
		{
			_lwBitmap = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
			SetStyle(ControlStyles.UserPaint, true);
			SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
			SetStyle(ControlStyles.AllPaintingInWmPaint, true);
			this.FlatStyle = FlatStyle.Flat;
			this.FlatAppearance.BorderSize = 0;
			this.BackColor = Color.Transparent;
			this.Text = "";

		}

		protected override void OnResize(EventArgs e)
		{
			base.OnResize(e);
			this._lwBitmap = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
			_lwGraphics = Graphics.FromImage(_lwBitmap);
		}


		protected override void OnPaintBackground(PaintEventArgs pevent)
		{
			//base.OnPaintBackground(pevent);
		}
		protected override void OnPaint(PaintEventArgs pevent)
		{
			
			if (DesignMode)
			{
				if (pevent == null)
					pevent = new PaintEventArgs(CreateGraphics(), new Rectangle(Location, Size));
				base.OnPaint(pevent);

				return;
			}
			if (_lwTransparent && BackgroundImage != null)
			{

				this._lwBitmap = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
				_lwGraphics = Graphics.FromImage(_lwBitmap);

				switch (this.BackgroundImageLayout)
				{
					case ImageLayout.Center:
						DrawCenter();
						break;
					case ImageLayout.None:
						DrawCenter();
						break;
					case ImageLayout.Stretch:
						DrawStretch();
						break;
					case ImageLayout.Tile:
						DrawCenter();
						break;
					case ImageLayout.Zoom:
						DrawCenter();
						break;
				}
			}
			else
			{
				if (pevent != null)
				{
					PaintEventArgs peventx = new PaintEventArgs(Graphics.FromImage(_lwBitmap), pevent.ClipRectangle);
					base.OnPaint(peventx);
				}
			}


		}


		bool _lwMouseDown = false;
		protected override void OnMouseDown(MouseEventArgs mevent)
		{
			base.OnMouseDown(mevent);
			_lwMouseDown = true;
			Parent.Update();
		}
		protected override void OnMouseUp(MouseEventArgs mevent)
		{
			base.OnMouseUp(mevent);
			_lwMouseDown = false;

		}
		

		private Point _lwPressShift=new Point(1,1);

		public Point LWPressShift
		{
			get { return _lwPressShift; }
			set { _lwPressShift = value; }
		}



		internal bool _lwDragForm = false;
		public bool LWDragForm
		{
			get
			{
				return _lwDragForm;
			}
			set
			{
				_lwDragForm = value;
			}
		}

		private void DrawCenter()
		{
			if (_lwMouseDown)
				_lwGraphics.DrawImage(BackgroundImage, _lwPressShift .X+ (Width - BackgroundImage.Width) / 2, _lwPressShift.Y + (Height - BackgroundImage.Height) / 2, BackgroundImage.Width, BackgroundImage.Height);
			else
				_lwGraphics.DrawImage(BackgroundImage, (Width - BackgroundImage.Width) / 2, (Height - BackgroundImage.Height) / 2, BackgroundImage.Width, BackgroundImage.Height);
		}

		private void DrawStretch()
		{
			if (_lwMouseDown)
				_lwGraphics.DrawImage(BackgroundImage, _lwPressShift.X, _lwPressShift.Y, Width - _lwPressShift.X, Height - _lwPressShift.Y);
			else
				_lwGraphics.DrawImage(BackgroundImage, 0, 0, Width - _lwPressShift.X, Height - _lwPressShift.Y);

		}

		protected override void WndProc(ref Message m)
		{

			if (m.Msg == Win32.WM_PAINT)
			{
				OnPaint(null);
			}
			if (m.Msg == Win32.WM_LBUTTONDOWN || m.Msg == Win32.WM_LBUTTONDBLCLK)
			{

				//Int32 pos=System.Runtime.InteropServices.Marshal.ReadInt32(m.LParam);
				Int32 x;
				Int32 y;

				y = Utils.SignedHIWORD(m.LParam);
				x = Utils.SignedLOWORD(m.LParam);



				Point p = new Point(Left + x, Top + y);
				//BitmapData bd=_lwBitmap.LockBits(new Rectangle(x, y, 1, 1), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
				//if (System.Runtime.InteropServices.Marshal.ReadByte(bd.Scan0, 0) > 0)
				//if (x>=0 && y>=0 && _lwBitmap.Width>=x && _lwBitmap.Height>=y && _lwBitmap.GetPixel(x, y).A > 0)
				this.GetType();
				if (_lwBitmap.GetPixel(x, y).A > 0)
				{
					//    _lwBitmap.UnlockBits(bd);
					base.WndProc(ref m);
					return;
				}
				else
				{
					/*if (false && Parent is LWSkinableForm)
					 {
						 //((LWSkinableForm)Parent).DoMouseDown(this)
						 //Console.WriteLine(this.Parent.GetChildAtPoint(new Point(Left + x, Top + y)));
						 //Win32.PostMessageSafe(new System.Runtime.InteropServices.HandleRef(c, c.Handle), Win32.WM_LBUTTONDOWN, (IntPtr)Win32.MKS.MK_LBUTTON, Utils.MAKELPARAM(-c.Left + Left + x, -c.Top + Top + y));
					 }
					 else
					 {*/

					bool foundThis = false;
					bool clickSent = false;
					foreach (Control c in this.Parent.Controls)
					{
						//Console.WriteLine(c);
						if (c.Equals(this))
						{
							foundThis = true;
							//Console.WriteLine("FOUND THIS");
						}
						else
							if (foundThis && c.Bounds.Contains(p))
							{
								//Console.WriteLine("SEND CLICK DOWN {0}", c);
								Win32.PostMessageSafe(new System.Runtime.InteropServices.HandleRef(c, c.Handle), Win32.WM_LBUTTONDOWN, (IntPtr)Win32.MKS.MK_LBUTTON, Utils.MAKELPARAM(-c.Left + Left + x, -c.Top + Top + y));
								clickSent = true;
								break;
							}
					}
					if (!clickSent)
						Win32.PostMessageSafe(new System.Runtime.InteropServices.HandleRef(Parent, Parent.Handle), Win32.WM_NCHITTEST, (IntPtr)Win32.MKS.MK_LBUTTON, Utils.MAKELPARAM(Left + x, Top + y));
					/*}*/
					//  _lwBitmap.UnlockBits(bd);
					return;
				}


			}

			base.WndProc(ref m);
		}



		protected override void DefWndProc(ref Message m)
		{
			base.DefWndProc(ref m);
		}

















		public override Image BackgroundImage
		{
			get
			{
				return base.BackgroundImage;
			}
			set
			{
				base.BackgroundImage = value;
			}
		}


		#region ILWControl Members
		[System.ComponentModel.Browsable(false)]
		System.Drawing.Bitmap ILWControl.LWBitmap
		{
			get
			{
				/*    if (this.Image != null)
					{
						//this.BackgroundImageLayout==ImageLayout.Stretch
						//if (this.BackgroundImage!=null)                    return (Bitmap)this.BackgroundImage;
						//_lwBitmap = (Bitmap)this.Image;
						_lwBitmap = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
						Graphics g = Graphics.FromImage(_lwBitmap);

						g.DrawImage(Image, 0, 0, Image.Width, Image.Height); 
						Console.WriteLine("-- {0} {1}",this.Image.Width, _lwBitmap.PhysicalDimension);

						//return (Bitmap)this.Image;
					}*/
				return _lwBitmap;
			}
		}
		

		



		private bool _lwTransparent = true;
		public bool LWTransparent
		{
			get
			{
				return _lwTransparent;
			}
			set
			{
				_lwTransparent = value;
			}
		}





		public Color LWTransparentColor
		{
			get
			{
				return Color.Transparent;
			}
			set
			{

			}
		}

		public bool LWTransparency
		{
			get
			{
				return true;
			}
		}

		


		public Color LWDesiredBackColor
		{
			get
			{
				return Color.Transparent;
			}
			set
			{
				
			}
		}

		#endregion
	}
}
